home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11400 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.2 KB

  1. Path: hubcap.clemson.edu!hubcap!mjs
  2. From: mjs@hubcap.clemson.edu (M. J. Saltzman)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: operator % - compiler error
  5. Date: 21 Mar 96 15:51:18 GMT
  6. Organization: Clemson University
  7. Message-ID: <mjs.827423478@hubcap>
  8. References: <4ini8g$i1u@spanky.pls.ov.com> <4iro6q$2e9@nntp.interaccess.com>
  9. NNTP-Posting-Host: hubcap.clemson.edu
  10. X-Newsreader: NN version 6.5.0 #1
  11.  
  12. brianmcg@interaccess.com (Brian V. McGroarty) writes:
  13.  
  14. >This is something I've wondered about.  Why isn't the mod operator defined
  15. >for floating point math?  It can be implemented easily enough via something
  16. >like:
  17.  
  18. >#define MOD( base, op ) ( (base) - ( (base) / (op) ) * (op) )
  19.  
  20. You need to cast the quotient to int before remultiplying.  Otherwise
  21. the result will be zero (up to rounding error).
  22.  
  23. >..so it does have validity, however the efficiency of using the remainder
  24. >on FPUs returning the remainder along with the result of a divide is lost
  25. >-- a spurious subtraction and multiplication are added. 
  26.  
  27. FPUs as a rule don't return an integer quotient and remainder, they
  28. return a floating point result.
  29.  
  30. If you want the floating-point remainder, use the fmod() function
  31. declared in math.h.
  32. -- 
  33.         Matthew Saltzman
  34.         Clemson University Math Sciences
  35.         mjs@clemson.edu
  36.